package com.zixi.ai.framework.service;

import com.zixi.ai.framework.dao.BaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 基础Service
 * @author linshuai
 */
public class BaseServiceImpl<T, ID> implements BaseService<T, ID> {
	@Autowired
	protected BaseRepository<T, ID> baseRepository;

	@Override
	public List<T> findAll() {
		return baseRepository.findAll();
	}
	
	@Override
	public List<T> findAll(Sort sort) {
		return baseRepository.findAll(sort);
	}

	@Override
	public List<T> findAll(Specification<T> spec) {
		return baseRepository.findAll(spec);
	}
	
	@Override
	public List<T> findAll(Specification<T> spec, Sort sort) {
		return baseRepository.findAll(spec, sort);
	}

	@Override
	public Page<T> findPage(Pageable pageable) {
		return baseRepository.findAll(pageable);
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void save(T t) {
		baseRepository.saveAndFlush(t);
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void saveAll(List<T> data) {
		baseRepository.saveAll(data);
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void del(ID id) {
		baseRepository.deleteById(id);
	}

//	@Override
//	public void delFake(Long id) {
//		baseRepository.delFake(id, Constant.DELETE_VALUE);
//	}

	@Override
	public Optional<T> findById(ID id) {
		return baseRepository.findById(id);
	}
	
	@Override
	public Page<T> findPage(Specification<T> spec, Pageable pageable) {
		return baseRepository.findAll(spec, pageable);
	}

	@Override
	public <M> Page<M> findPage(String sql, Class<M> cls, Pageable pageable) {
		return baseRepository.findPage(sql, cls, pageable);
	}
	
	@Override
	public Page<T> findPage(Specification<T> spec, Pageable pageable, Sort sort) {
		return baseRepository.findAll(spec, pageable, sort);
	}

	@Override
	public <M> List<M> findAll(String sql, Class<M> cls) {
		return baseRepository.findAll(sql, cls);
	}

	@Override
	public Optional<T> findOne(Specification<T> spec) {
		return baseRepository.findOne(spec);
	}

    @Override
    public long count() {
		return baseRepository.count();
    }

	@Override
	public long count(Specification<T> spec) {
		return baseRepository.count(spec);
	}

	@Override
	public List<T> findAll(String hql, Object... params) {
		return baseRepository.findAll(hql, params);
	}

	@Override
	public <M> List<M> findAllM(String hql, Object... params) {
		return baseRepository.findAllM(hql, params);
	}
	@Override
	public void delAll(List<T> data) {
		if(data != null && !data.isEmpty()) {
			baseRepository.deleteAll(data);
		}

	}
}